

package com.yh.correct.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.yh.correct.model.vo.CustomException;



/**
 * 全局异常处理器
 * @author Administrator
 *
 */
public class GlobalExceptionHandler implements HandlerExceptionResolver {
	
	/**
	 * 日志
	 */
	private static Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);

	/**
	 * 异常跳转页码
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		// TODO Auto-generated method stub
		ex.printStackTrace();
		String message = null;
		CustomException customException = null;
		if(ex instanceof CustomException){
			customException = (CustomException)ex;
		}else{
			customException = new CustomException("未知错误");
		}
		//错误 信息
		message = customException.getMessage();
		request.setAttribute("message", message);
		try {
			//转向到错误 页面
			request.getRequestDispatcher("error.html").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView(); 
	}

}
